//
//  KTTagListView.m
//  JCTagListView
//
//  Created by 宽图 on 15/9/29.
//

#import "KTTagListView.h"
//#import "Macro.h"
#define VIEW_WIDTH(v) (v.frame.size.width)
#define VIEW_HEIGHT(v) (v.frame.size.height)

@implementation NSObject(KTTagListView)

- (CGFloat)minimumHeightSpacingForRowInTagListView:(KTTagListView *)tagListView {
    return 5;
}
- (CGFloat)minimumWidthSpacingForCellInTagListView:(KTTagListView *)tagListView {
    return 5;
}

- (void)tagListView:(KTTagListView *)tagListView willDisplayCell:(UIButton *)button AtIndex:(NSInteger)index {
    return;
}
- (void)tagListView:(KTTagListView *)tagListView didSelectAtIndex:(NSInteger)index {
    return;
}

- (UIEdgeInsets)edgetInsetsForTagListView:(nullable KTTagListView *)tagListView {
    return UIEdgeInsetsZero;
}

- (KTTagListItemArrangeDirection)itemArrangeDirectionOftagListView:(nullable KTTagListView *)taglistView {
    return KTTagListItemArrangeDirectionLeftToRight;
}


@end

@interface KTTagListView()
@property (nonatomic, strong) NSMutableArray *tagListArray;
@end


@implementation KTTagListView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self setUp];
//        self.initFlag = false;
//        [self setUpFrame];
    }
    return self;

}
- (instancetype)initWithCoder:(NSCoder *)aDecoder {
    self = [super initWithCoder:aDecoder];
    if (self) {
        [self setUp];
//          self.initFlag = false;
    }
    return self;
}


#pragma mark -
#pragma mark Setter And Getter
- (NSMutableArray *)tagListArray {
    if (!_tagListArray) {
        _tagListArray = [[NSMutableArray alloc] init];
    }
    return _tagListArray;
}
- (void)setDelegate:(id<KTTagListViewDelegate>)delegate {
    if (delegate != _delegate) {
        _delegate = delegate;
//        [self setUpFrame];
    }
}
- (void)setDataSource:(id<KTTagListViewDataSource>)dataSource {
    if (dataSource != _dataSource) {
        _dataSource = dataSource;
//        [self setUpFrame];
    }
}

- (void)layoutSubviews {
    [self setUpFrame];
}
- (void)didMoveToSuperview {

}

- (void)setUp {
    self.backgroundColor = [UIColor whiteColor];
}
- (void)setUpFrame {

}

- (void)drawRect:(CGRect)rect {
//#warning temporary solution need reconstruct
  
    for (UIView *view in self.subviews) {
        [view removeFromSuperview];
    }
    
    CGFloat frontButtonsWidth = 0;
    CGFloat frontButtonsHeight = 0;
    UIEdgeInsets edgeInsets = [self.delegate edgetInsetsForTagListView:self];
    UIScrollView *contentScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(edgeInsets.left, edgeInsets.top, VIEW_WIDTH(self) - edgeInsets.left - edgeInsets.right, VIEW_HEIGHT(self) - edgeInsets.top- edgeInsets.bottom)];
    contentScrollView.bounces = NO;
    contentScrollView.tag = 100;
    contentScrollView.showsHorizontalScrollIndicator = NO;
    CGFloat lastButtonHeight = 0;
    for (int i = 0; i < [self.dataSource numberOfItemsInTagListView:self]; i++) {
        UIButton *button = [self.dataSource tagListView:self cellAtIndex:i];
        
        [self.tagListArray removeAllObjects];
        [self.tagListArray addObject:button];
        
        [button sizeToFit];
        [self.delegate tagListView:self willDisplayCell:button AtIndex:i];
        
        if (frontButtonsWidth + VIEW_WIDTH(button) >= VIEW_WIDTH(contentScrollView)) {
            frontButtonsWidth = 0;
            frontButtonsHeight += VIEW_HEIGHT(button) + [self minimumHeightSpacingForRowInTagListView:self];
        }
        CGRect theFrame = button.frame;
        if ([self.delegate itemArrangeDirectionOftagListView:self] == KTTagListItemArrangeDirectionLeftToRight) {
            theFrame.origin.x = frontButtonsWidth;
        } else {
            theFrame.origin.x = VIEW_WIDTH(contentScrollView) - VIEW_WIDTH(button) - frontButtonsWidth;
        }
        
        theFrame.origin.y = frontButtonsHeight;
        theFrame.size.width = VIEW_WIDTH(button);
        theFrame.size.height = VIEW_HEIGHT(button);
        button.frame = theFrame;
        button.tag = i;
        [button addTarget:self action:@selector(clickEventForButton:) forControlEvents:UIControlEventTouchUpInside];
        [contentScrollView addSubview:button];
        
        frontButtonsWidth += VIEW_WIDTH(button) + [_delegate minimumWidthSpacingForCellInTagListView:self];
        lastButtonHeight = VIEW_HEIGHT(button);
    }
    
    contentScrollView.contentSize = CGSizeMake(VIEW_WIDTH(contentScrollView), frontButtonsHeight + lastButtonHeight);
    [self addSubview:contentScrollView];
}
- (void)clickEventForButton:(UIButton *)button {
    [self.delegate tagListView:self didSelectAtIndex:button.tag];
}

- (void)reloadData {
    [self setNeedsDisplay];
}
@end
